package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class KafkaListenerImpl {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = "downOrUpArticle")
    public void upOrDown(String massage) {
        log.info("已经收到消息{}",massage);
        Map map = JSON.parseObject(massage, Map.class);
        String articleId = (String) map.get("articleId");
        String status = (String) map.get("status");
        Long id = Long.valueOf(articleId);
        LambdaQueryWrapper<ApArticleConfig> apArticleConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apArticleConfigLambdaQueryWrapper.eq(ApArticleConfig::getArticleId, id);
        ApArticleConfig one = apArticleConfigService.getOne(apArticleConfigLambdaQueryWrapper);
        if ("0".equals(status)) {
            one.setIsDown(false);
        } else {
            one.setIsDown(true);
        }

        apArticleConfigService.updateById(one);
    }

}
